
package org.okkam.refine.strategy;


import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.okkam.client.data.AttributeMetadataType;
import org.okkam.client.data.AttributeType;
import org.okkam.client.data.AttributesType;
import org.okkam.client.data.Entity;
import org.okkam.client.data.ProfileType;
import org.okkam.client.util.XMLEntityConverter;
import org.okkam.refine.strategy.ArtifactCandidate;
import org.okkam.refine.strategy.CandidateStrategy;

public class ArtifactCandidate implements CandidateStrategy {

    private static Log log = LogFactory.getLog(ArtifactCandidate.class);

    @Override
    public boolean checkEquality(List<AttributeType> attributeTypeList, List<String> entityList) {

        Iterator<AttributeType> attributeTypeIterator = attributeTypeList.iterator();
        Map<String, String> propValueNewEntity = new HashMap<String, String>();
        boolean match = false;

        while (attributeTypeIterator.hasNext()) {
            AttributeType attributeType = attributeTypeIterator.next();
            String term = attributeType.getName().getLocalPart().toLowerCase().replace("\"", "");
            String value = attributeType.getValue().toLowerCase().replace("\"", "");
            attributeType.getMetadata();
            propValueNewEntity.put(term, value);
        }

        Iterator entityIterator = entityList.iterator();
        // for every candidate responses from the ENS, compare with the New
        // Entity to be created
        while (entityIterator.hasNext()) {
            XMLEntityConverter converter = new XMLEntityConverter();
            Entity entity = null;
            try {
                entity = converter.xmlToEntity(entityIterator.next().toString());
            } catch (JAXBException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            ProfileType profile = entity.getProfile();
            AttributesType attributesType = profile.getAttributes();
            List<AttributeType> attributeTypeListResponse = attributesType.getAttributes();
            Iterator<AttributeType> attributeTypeIteratorresponse = attributeTypeListResponse.iterator();

            Map<String, String> propValueResponse = new HashMap<String, String>();

            while (attributeTypeIteratorresponse.hasNext()) {
                AttributeType attributeType = attributeTypeIteratorresponse.next();
                String term = attributeType.getName().getLocalPart().toLowerCase().replace("\"", "");
                String value = attributeType.getValue().toLowerCase().replace("\"", "");
                propValueResponse.put(term, value);
            }

            Iterator<String> it = propValueNewEntity.keySet().iterator();
            match = false;
            
            while (it.hasNext()&&(propValueNewEntity.size() == propValueResponse.size())) {
                String property = it.next();
                if (propValueResponse.containsKey(property)) {
                    if (propValueNewEntity.get(property).equals(
                            propValueResponse.get(property))) {
                        match = true;
                    } else {
                        match = false;
                        break; // check other candidates, if any
                    }
                } else {
                    match = false;
                    break;// check other candidate;
                }
            }
            if (match) {
                return match;
            }
        }
        return match;
    }
}
